//
//  JPDiseaseList.m
//  JPKY
//
//  Created by 周述坚 on 16/11/15.
//  Copyright © 2016年 Vitas. All rights reserved.
//

#import "JPDiseaseListVC.h"
#import "InterfaceTool.h"
#import "JPDiseaseListModel.h"

@interface JPDiseaseListVC ()

@property (nonatomic,strong) NSArray<JPDiseaseListModel *> *diseaseListArr;

@property (nonatomic,copy) NSString *titleText;

/**
 *  治疗手段数组
 */
@property (nonatomic,strong)NSArray *treatArr;

@end

@implementation JPDiseaseListVC {
    BOOL _isSelect;
    NSInteger _numbel;
    BOOL _isSe;
    NSInteger count;
    NSInteger _num;
}

- (void)viewDidLoad {
    [super viewDidLoad];
    
    self.treatArr = @[@"手术治疗",@"保守治疗",@"药物治疗"];
    
    self.titleText = @"";
    _num = self.tag;
    if (_num == 1000) {
        [JPDiseaseListModel loadDataWithDataSuccess:^(NSArray *newsList) {
            // 直接复制
            self.diseaseListArr = newsList;
            // 刷新列表
            [ [NSOperationQueue mainQueue] addOperationWithBlock:^{
                [self.tableView reloadData];
            }];
        } failed:^(NSError *error) {
            NSLog(@"%@",error);
        }];
    } else {
        [JPDiseaseListModel loadDataWithURLStr:_num success:^(NSArray *newsList) {
            // 直接复制
            self.diseaseListArr = newsList;
            // 刷新列表
            [ [NSOperationQueue mainQueue] addOperationWithBlock:^{
                [self.tableView reloadData];
            }];
        } failed:^(NSError *error) {
            NSLog(@"%@",error);
        }];
    }
    _isSelect = NO;
    _numbel = 0;
    [self setNavigationBar];
}

/**
 *  设置导航
 */
- (void)setNavigationBar {
    UIBarButtonItem *makeSureButtonItem = [[UIBarButtonItem alloc] initWithTitle:@"确定" style:UIBarButtonItemStylePlain target:self action:@selector(buttonItemAction)];
    self.navigationItem.rightBarButtonItem  = makeSureButtonItem;
    
}

/**
 *  点击导航栏确认响应按钮
 */
- (void)buttonItemAction {
    self.completion(self.titleText);
    [self.navigationController popViewControllerAnimated:YES];
}

#pragma mark - Table view data source
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
    if(self.tag == 1001) {
        return self.treatArr.count;
    } else {
        return self.diseaseListArr.count;
    }
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
    NSInteger num = self.tag;
    
    // 创建单元格
    UITableViewCell *cell =  [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:@"diseaseCell"];
    // 当标签为1001时,显示指定数组
    if (num == 1001) {
        cell.accessoryType = UITableViewCellAccessoryDisclosureIndicator;
        cell.textLabel.text = self.treatArr[indexPath.row];
    } else {
        cell.accessoryType = UITableViewCellAccessoryDisclosureIndicator;
        cell.textLabel.text = self.diseaseListArr[indexPath.row].ci3_name;
        if (cell.textLabel.text == nil) {
            cell.textLabel.text = self.diseaseListArr[indexPath.row].complication_name;
        }
    }
    return cell;
}

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
    NSString *title = self.diseaseListArr[indexPath.row].ci3_name;
    NSString *title02 = self.diseaseListArr[indexPath.row].complication_name;
    
    if (_num == 1000) {
        // 判断是不是点击的原来的按钮
        if (_numbel != indexPath.row) {
            UIImageView *imageView = [[UIImageView alloc] initWithImage:[UIImage imageNamed:@"imageSelected"]];
            [tableView cellForRowAtIndexPath:indexPath].accessoryView = imageView;
            _isSe = YES;
            count ++;
            self.titleText = [NSString stringWithFormat:@"%@ %@",self.titleText,title02];
            
        } else {
            if (_isSe == YES) {
                [tableView cellForRowAtIndexPath:indexPath].accessoryView = nil;
                _isSe = NO;
                count --;
            } else {
                UIImageView *imageView = [[UIImageView alloc] initWithImage:[UIImage imageNamed:@"imageSelected"]];
                [tableView cellForRowAtIndexPath:indexPath].accessoryView = imageView;
                self.titleText = [NSString stringWithFormat:@"%@,%@",self.titleText,title02];
                _isSe = YES;
                count ++;
            }
        }
        _numbel = indexPath.row;
        
    } else if (_num == 1001) {
        NSString *title03 = self.treatArr[indexPath.row];
        [self.navigationController popViewControllerAnimated:YES];
        self.completion(title03);
    } else {
        self.completion(title);
        [self.navigationController popViewControllerAnimated:YES];
    }
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

@end
